package org.liaohailong.library.http;

import androidx.annotation.NonNull;

import java.lang.reflect.Type;

/**
 * Author: liaohailong
 * Date: 2022/8/12
 * Time: 20:31
 * Description: 网络请求回调适配器，为了取出泛型
 **/
public class AbHttpCallback<T> implements IHttpCallback<T> {

    @Override
    public void onRawData(@NonNull String json) {
        Type[] classType = HttpHelper.getClassType(this, AbHttpCallback.class);

        // 子线程解析json
        HttpHelper.post2work(() -> {
            T data = HttpHelper.sGson.fromJson(json, classType[0]);
            // 回调主线程处理数据
            HttpHelper.post2main(() -> onResponse(data));
        });
    }

    @Override
    public void onProgress(long currentCount, long totalCount) {
        // 文件 上传/下载 的进度回调
    }

    @Override
    public void onDownload(@NonNull String path) {
        // 文件下载完毕后回调
    }

    @Override
    public void onFailure(@NonNull Exception exception) {

    }

    protected void onResponse(T rawData) {

    }
}
